/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/util/TableNode.fx $
******************************************************************************/
package de.hft_stuttgart.robofight.client.javafx.util;

import javafx.animation.transition.TranslateTransition;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;

/**
 * @author ralf.dauenhauer@gmx.de
 */

public class TableNode extends CustomNode {

    public var getNodeByName : function(nodeName : String) : Node;
    public var tableEntryLayout : String;
    public var items: TableEntry[];

    public var w: Number;
    public var h: Number;

    var tableArea: Rectangle;
    public var tableEntries: Object[];
    public var tableEntriesGroup: Group;
    public var scrollStepSize : Integer = 14;
    var firstEntry = 0;

    var scrollUpButton : ScrollButton;
    var scrollDownButton : ScrollButton;

    override public function create():Node {
        var centerX = w / 2;
        var centerY = h / 2;
        var buttonHeight = 12;
        var arrowWidth = 20;
        scrollUpButton = ScrollButton {
            h: buttonHeight
            y: - 10
            action: scrollDown
            arrow: Polygon {
                fill: Color.rgb(23, 185, 241);
                points: [
                    centerX - arrowWidth, buttonHeight - 4,
                    centerX + arrowWidth, buttonHeight - 4,
                    centerX, 3
                ]
            }
        }

        tableArea = Rectangle {
            width: w
            height: h
            //y : 15
        }
        scrollDownButton = ScrollButton {
            y: h + 2
            h: buttonHeight
            action: scrollUp
            arrow: Polygon {
                fill: Color.rgb(23, 185, 241);
                points: [
                    centerX - arrowWidth, 3,
                    centerX + arrowWidth, 3,
                    centerX, buttonHeight - 4
                ]
            }
        }

        tableEntriesGroup = Group {
            content: bind tableEntries as Node[]
        }

        enableScroll();
        Group {
            content: [
                Group {
                    content: [tableEntriesGroup]
                    clip: tableArea
                }
                scrollUpButton,
                scrollDownButton
            ]
        }
    }

    function enableScroll():Void {
        scrollDownButton.scrolling = true; //(firstEntry + 1) <  sizeOfTableEntries;
        scrollUpButton.scrolling = firstEntry > 0;
    }

    function scroll(amount:Number) {
        scrollUpButton.scrolling = false;
        scrollDownButton.scrolling = false;
        TranslateTransition {
            node: tableEntriesGroup
            byY: amount
            duration: 0.3s
            action: function() { enableScroll() }
        }.play();
    }

    public var scrollUp = function():Void {
        scroll(-(tableEntries[firstEntry] as Node).boundsInLocal.height -scrollStepSize);
        firstEntry++;
    }

    public var scrollDown = function():Void {
        scroll((tableEntries[firstEntry - 1] as Node).boundsInLocal.height + scrollStepSize);
        firstEntry--;
    }
}
